package com.smooth.site.sx.pc.jifen.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.common.site.common.utils.IntegerUtils;
import com.smooth.common.site.modules.sys.entity.User;
import com.smooth.common.site.modules.sys.utils.UserUtils;
import com.smooth.dao.gen.BizJifenLogMapper;
import com.smooth.dao.gen.BizStudentMapper;
import com.smooth.dao.gen.bean.BizJifenLog;
import com.smooth.dao.gen.bean.BizJifenLogExample;
import com.smooth.site.sx.pc.jifen.enums.JifenType;

@Component
public class PcJifenLogService {
	Logger log = LoggerFactory.getLogger(PcJifenLogService.class);
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	BizJifenLogMapper bizJifenLogMapper;
	@Autowired
	BizStudentMapper bizStudentMapper;
	public void insertJifenLog(Integer jifenAmount, Integer changeAmount, JifenType jifenType, String uid, String uname) {
		insertJifenLog(jifenAmount, changeAmount, jifenType, uid, uname, null, null);
	}
	@Transactional
	public void insertJifenLog(Integer jifenAmount, Integer changeAmount, JifenType jifenType, String uid, String uname,String orderNo,String content) {
		Date date = new Date();

		int jifenAmountAfter = IntegerUtils.add(jifenAmount, changeAmount);
		BizJifenLog record = new BizJifenLog();
		record.setId(idGenerater.genClsNo("JL"));
		record.setJifenAddAmount(changeAmount);
		record.setJifenAmountAfter(jifenAmountAfter);
		record.setJifenAmount(jifenAmount);
		record.setJifenType(jifenType.value);
		record.setJifenTypeName(jifenType.name);

		record.setOrderNo(orderNo);
		record.setContent(content);
		
		record.setUserId(uid);
		record.setUserName(uname);

		User user = UserUtils.getUser();
		if (user != null) {
			record.setOpdName(user.getName());
			record.setOptId(user.getId());
		}

		record.setUpdateDate(date);
		record.setCreateDate(date);
		int optNum = bizJifenLogMapper.insertSelective(record);
		if (optNum == 1) {
			log.info("【" + uid + "】添加了积分日志【" + jifenAmount + "】");
		} else {
			throw new SiteException(SiteCodeEnums.DB_ERROR, uid + "");
		}
	}
	
	public List<BizJifenLog> queryJifenLog(String stuId){
		BizJifenLogExample bizJifenLogExample = new BizJifenLogExample();
		BizJifenLogExample.Criteria criteria = bizJifenLogExample.createCriteria();
		
		criteria.andUserIdEqualTo(stuId);
		bizJifenLogExample.setOrderByClause(" create_date desc ");
		List<BizJifenLog> blist = bizJifenLogMapper.selectByExample(bizJifenLogExample);
		return blist;
	}
}
